package com.anuo.app.modules.study.studyjson;


import com.anuo.app.modules.study.entity.Staff;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/* jackson 常见操作
* 研究状态: 已研究完
* @java to json
* @json to java
* @java to/from json 复杂类型(多层数据)
* @java to/from file
* @原生json 树操作  http://www.yiibai.com/jackson/jackson_streaming_api.html
*                  http://www.yiibai.com/jackson/jackson_tree_model.html
* 参考: http://www.yiibai.com/jackson/
* */
public class StudyJackson {
    public static ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) {
        try {
            //javaToJson();
            //jsonToJava();
            javaToJsonComplex();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //java to json
    public static void javaToJson() throws Exception {
        Staff staff = createStaff();
        List<Staff> listStaff = new ArrayList<Staff>();
        listStaff.add(staff);
        String staffJsonStr = objectMapper.writeValueAsString(listStaff/*or staff 都可以*/);
        System.out.println(staffJsonStr);
        //格式化漂亮输出
        String prettyJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(staff);
        //System.out.println(prettyJson);
    }

    //json to java
    public static void jsonToJava() throws Exception {
        String jsonStr = "{\"name\":\"jk\"}";
        Staff staff = objectMapper.readValue(jsonStr, Staff.class);
        //json to java list
        String jsonArray = "[{\"name\":\"jk\"},{\"name\":\"anuo\"}]";
        List<Staff> listStaff = objectMapper.readValue(jsonArray, new TypeReference<List<Staff>>() {
        });
        //url json to java
        Staff staff1 = objectMapper.readValue(new URL("http://mkyong.com/api/staff.json"), Staff.class);

    }

    //java to/from json 复杂类型
    public static void javaToJsonComplex() throws Exception {
//        StudentData studentData = new StudentData();
//        Student student = new Student();
//        student.setName("jk");
//        student.setAge(21);
//        studentData.setStudent(student);
//        studentData.setName("学生数据");
//        studentData.setVerified(true);
//        int[] marks = {1, 69, 5};
//        studentData.setMarks(marks);
//        //java to json 复杂类型
//        String json = objectMapper.writeValueAsString(studentData);
//        String jsonFormat = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(studentData);
//        //System.out.println(jsonFormat);
//
//        //json 复杂类型 to java
//        StudentData studentData1 = objectMapper.readValue(json, new TypeReference<StudentData>() {
//        });
    }

    //java to/from File
    public static void jsonToFromFile() throws Exception {
        //java obj to file
        Staff staff = createStaff();
        objectMapper.writeValue(new File("c:\\json.txt"), staff);
        //file to java obj
        staff = objectMapper.readValue(new File("C:\\json.txt"), Staff.class);
    }

    public static Staff createStaff() {
        Staff staff = new Staff();
        staff.setName("anuo");
        staff.setAge(27);
        staff.setPosition("成都");
        staff.setSkills(new ArrayList<String>(Arrays.asList("cao", "hehe", "java")));
        return staff;
    }

}


